﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

namespace Macaw.Moss2007.ContentCopySolution
{
    public class ContentCopy : Microsoft.SharePoint.WebControls.LayoutsPageBase
    {
        protected HyperLink hlBrokenLinks;

        const string CONTENTCOPY_JOB_NAME = "ContentCopy";

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected void btnCheck_OnClick(object sender, EventArgs e)
        {
            ContentCopyJobDefinition checkjob = null;
            // make sure the job isn't already registered
            foreach (SPJobDefinition job in SPContext.Current.Site.WebApplication.JobDefinitions)
            {
                if (job.Name == CONTENTCOPY_JOB_NAME)
                {
                    checkjob = (ContentCopyJobDefinition)job;
                }
            }

            if (checkjob != null)
            {
                using (SPLongOperation operation = new SPLongOperation(this.Page))
                {
                    operation.Begin();
                    try
                    {
                        checkjob.Execute(Site.ContentDatabase.Id);
                    }
                    finally
                    {

                        operation.End(Request.Url.ToString(), SPRedirectFlags.UseSource, this.Context, null);
                    }
                }
            }
        }

        protected override void CreateChildControls()
        {
            try
            {
                SPList bllist = SPContext.Current.Site.RootWeb.GetList("/Lists/ContentCopy");
                hlBrokenLinks.NavigateUrl = bllist.DefaultViewUrl;
            }
            catch (Exception)
            {
            }
            
            base.CreateChildControls();
        }
    }
}
